$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
$.extend({
    'crudgrid': {
        'getGrid': function (gridBtn) {
            return $(gridBtn).parent().closest('div.datagrid-wrap').children('div.datagrid-view').children('table.datagrid-f');
        },
        'buttons': {
            'create': {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    var $grid = $.crudgrid.getGrid(this);
                    var $dialog = $('<div id=dialog />');
                    $dialog.appendTo('body');
                    var $options = $grid.datagrid('options');
                    var $param = {
                        title: '新增',
                        width: 260,
                        href: 'edit',
                        grid: $grid,
                        onClose: function () {
                            $dialog.dialog('destroy');
                        },
                        buttons: [$.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close]
                    };
                    if ($options.dialog) {
                        $param = $.extend($param, $options.dialog.create);
                    }
                    $dialog.dialog($param);
                }
            },
            'update': {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    var $grid = $.crudgrid.getGrid(this);
                    var row = $grid.datagrid('getSelected');
                    if (row == null) {
                        $.messager.alert({
                            title: '提示',
                            msg: '请先选择一行，再进行编辑操作',
                            icon: 'info'
                        });
                        return;
                    }
                    var $dialog = $('<div id="dialog"/>');
                    var $options = $grid.datagrid('options');
                    $dialog.appendTo($grid.parent());
                    var $param = {
                        title: '编辑',
                        width: 260,
                        href: 'edit',
                        grid: $grid,
                        queryParams: {id: row.id},
                        onClose: function () {
                            $dialog.remove();
                        },
                        buttons: [$.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close]
                    };
                    if ($options.dialog) {
                        $param = $.extend($param, $options.dialog.update);
                    }
                    $dialog.dialog($param);
                }
            },
            'delete': {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    var $table = $(this).parent().closest('.panel').find('.datagrid-f');
                    var row = $table.datagrid('getSelected');
                    var $dialog = $table.datagrid('options').dialog;
                    var $url = $dialog && $dialog.delete && $dialog.delete.href ? $dialog.delete.href:'delete';
                    if (row == null) {
                        $.messager.alert({
                            title: '提示',
                            msg: '请先选中一行，再进行删除操作！',
                            icon: 'info'
                        });
                        return;
                    }
                    $.messager.confirm('确认对话框', '您想要删除选中的数据吗？', function (r) {
                        if (r) {
                            $.post($url, {id: row.id}, function (result) {
                                if (result.success) {
                                    $.messager.show({
                                        title: '提示',
                                        msg: result.msg,
                                        icon: 'info'
                                    });
                                    if ($table.data('treegrid')) {
                                        $table.treegrid('reload')
                                    } else {
                                        $table.datagrid('reload');
                                    }
                                } else {
                                    $.messager.alert({
                                        title: '提示',
                                        msg: result.msg,
                                        icon: 'error'
                                    });
                                }
                            });
                        }
                    });
                }
            },
            'search': {
                text: '查询',
                iconCls: 'icon-search',
                handler: function () {
                    var $grid = $.crudgrid.getGrid(this);
                    var $param = $grid.datagrid('options').queryParams;
                    var $dialog = $("<div/>");
                    var $options = $grid.datagrid('options');
                    $dialog.appendTo($grid.parent());
                    var $pam = {
                        title: '查询',
                        width: 260,
                        href: 'search',
                        grid: $grid,
                        queryParams: $param,
                        onClose: function () {
                            $dialog.dialog('destroy');
                        },
                        buttons: [$.dialog.buttons.search, $.dialog.buttons.clear, $.dialog.buttons.close]
                    };
                    if ($options.dialog) {
                        $pam = $.extend($pam, $options.dialog.search);
                    }
                    $dialog.dialog($pam);
                }
            },
            'reload': {
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    var $table = $(this).parent().closest('.panel').find('.datagrid-f');
                    var row = $table.datagrid('getSelected');
                    $.messager.confirm('确认对话框', '确认刷新表格？', function (r) {
                        if (r) {
                            if ($table.data('treegrid')) {
                                if (row != null) {
                                    $table.treegrid('unselectAll');
                                }
                                $table.treegrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: "刷新成功",
                                    icon: 'info'
                                });
                            } else {
                                if (row != null) {
                                    $table.treegrid('unselectAll');
                                }
                                $table.datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: "刷新成功",
                                    icon: 'info'
                                });
                            }
                        }
                    });
                }
            },
            'huanwei': {
                text: '修改成环卫工人',
                iconCls: 'icon-tip',
                handler: function () {
                    var $table = $(this).parent().closest('.panel').find('.datagrid-f');
                    var row = $table.datagrid('getSelected');
                    var $dialog = $table.datagrid('options').dialog;
                    var $url = $dialog && $dialog.huanwei && $dialog.huanwei.href ? $dialog.huanwei.href:'huanwei';
                    if (row == null) {
                        $.messager.alert({
                            title: '提示',
                            msg: '请先选中一行，再进行操作！',
                            icon: 'info'
                        });
                        return;
                    }
                    $.messager.confirm('确认对话框', '确认添加成环卫工人？', function (r) {
                        if (r) {
                        	 $.post($url, {id: row.id}, function (result) {
                                 if (result.success) {
                                     $.messager.show({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'info'
                                     });
                                     if ($table.data('treegrid')) {
                                         $table.treegrid('reload')
                                     } else {
                                         $table.datagrid('reload');
                                     }
                                 } else {
                                     $.messager.alert({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'error'
                                     });
                                 }
                             });
                        }
                    });
                }
            },
            'user': {
                text: '修改成普通用户',
                iconCls: 'icon-tip',
                handler: function () {
                    var $table = $(this).parent().closest('.panel').find('.datagrid-f');
                    var row = $table.datagrid('getSelected');
                    var $dialog = $table.datagrid('options').dialog;
                    var $url = $dialog && $dialog.user && $dialog.user.href ? $dialog.user.href:'user';
                    if (row == null) {
                        $.messager.alert({
                            title: '提示',
                            msg: '请先选中一行，再进行操作！',
                            icon: 'info'
                        });
                        return;
                    }
                    $.messager.confirm('确认对话框', '确认添加成普通用户？', function (r) {
                        if (r) {
                        	 $.post($url, {id: row.id}, function (result) {
                                 if (result.success) {
                                     $.messager.show({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'info'
                                     });
                                     if ($table.data('treegrid')) {
                                         $table.treegrid('reload')
                                     } else {
                                         $table.datagrid('reload');
                                     }
                                 } else {
                                     $.messager.alert({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'error'
                                     });
                                 }
                             });
                        }
                    });
                }
            },
            'usher':{
            	text: '修改成引导员',
                iconCls: 'icon-tip',
                handler: function () {
                    var $table = $(this).parent().closest('.panel').find('.datagrid-f');
                    var row = $table.datagrid('getSelected');
                    var $dialog = $table.datagrid('options').dialog;
                    var $url = $dialog && $dialog.usher && $dialog.usher.href ? $dialog.usher.href:'usher';
                    if (row == null) {
                        $.messager.alert({
                            title: '提示',
                            msg: '请先选中一行，再进行操作！',
                            icon: 'info'
                        });
                        return;
                    }
                    $.messager.confirm('确认对话框', '确认添加成引导员？', function (r) {
                        if (r) {
                        	 $.post($url, {id: row.id}, function (result) {
                                 if (result.success) {
                                     $.messager.show({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'info'
                                     });
                                     if ($table.data('treegrid')) {
                                         $table.treegrid('reload')
                                     } else {
                                         $table.datagrid('reload');
                                     }
                                 } else {
                                     $.messager.alert({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'error'
                                     });
                                 }
                             });
                        }
                    });
                }
            },
            'wbljhsz':{
            	text: '修改为外部垃圾回收站',
                iconCls: 'icon-tip',
                handler: function () {
                    var $table = $(this).parent().closest('.panel').find('.datagrid-f');
                    var row = $table.datagrid('getSelected');
                    var $dialog = $table.datagrid('options').dialog;
                    var $url = $dialog && $dialog.wbljhsz && $dialog.wbljhsz.href ? $dialog.wbljhsz.href:'wbljhsz';
                    if (row == null) {
                        $.messager.alert({
                            title: '提示',
                            msg: '请先选中一行，再进行操作！',
                            icon: 'info'
                        });
                        return;
                    }
                    $.messager.confirm('确认对话框', '确认添加外部垃圾回收站？', function (r) {
                        if (r) {
                        	 $.post($url, {id: row.id}, function (result) {
                                 if (result.success) {
                                     $.messager.show({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'info'
                                     });
                                     if ($table.data('treegrid')) {
                                         $table.treegrid('reload')
                                     } else {
                                         $table.datagrid('reload');
                                     }
                                 } else {
                                     $.messager.alert({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'error'
                                     });
                                 }
                             });
                        }
                    });
                }
            },
            'ljfjz':{
            	text: '修改为垃圾分拣站',
                iconCls: 'icon-tip',
                handler: function () {
                    var $table = $(this).parent().closest('.panel').find('.datagrid-f');
                    var row = $table.datagrid('getSelected');
                    var $dialog = $table.datagrid('options').dialog;
                    var $url = $dialog && $dialog.ljfjz && $dialog.ljfjz.href ? $dialog.ljfjz.href:'ljfjz';
                    if (row == null) {
                        $.messager.alert({
                            title: '提示',
                            msg: '请先选中一行，再进行操作！',
                            icon: 'info'
                        });
                        return;
                    }
                    $.messager.confirm('确认对话框', '确认添加垃圾分拣站？', function (r) {
                        if (r) {
                        	 $.post($url, {id: row.id}, function (result) {
                                 if (result.success) {
                                     $.messager.show({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'info'
                                     });
                                     if ($table.data('treegrid')) {
                                         $table.treegrid('reload')
                                     } else {
                                         $table.datagrid('reload');
                                     }
                                 } else {
                                     $.messager.alert({
                                         title: '提示',
                                         msg: result.msg,
                                         icon: 'error'
                                     });
                                 }
                             });
                        }
                    });
                }
            }
            
        }
    }

});

$.extend({
    'dialog': {
        'me': function (dialogBtn) {
            return $(dialogBtn).closest('.window').find('div.window-body');
        },
        'getForm': function (dialogBtn) {
            var aa=$(dialogBtn == undefined ? this : dialogBtn).parent().closest('.window').find('form');
            return aa;
        },
        'buttons': {
            'reset': {
                text: '重置',
                iconCls: 'icon-reload',
                handler: function () {
                    var $form = $.dialog.getForm(this);
                    $form.form('reset');
                }
            },
            'save': {
                text: '保存',
                iconCls: 'icon-ok',
                handler: function () {
                    var $dialog = $.dialog.me(this);
                    var $form = $.dialog.getForm(this);
                    var $grid = $dialog.dialog('options').grid;
                    var $action = $dialog.dialog('options').action;
                    var fd = new FormData($form[0]);
                    if (!$form.form('validate')) {
                        return;
                    }
                    $.ajax({
                        url: $action?$action:"save",
                        cache: false,
                        type: 'post',
                        data: fd,
                        dataType: 'json',
                        processData: false,
                        contentType: false,
                        success: function (result) {
                            if (result.success) {
                                $.messager.show({
                                    title: '提示',
                                    msg: result.msg,
                                    icon: 'info'
                                });
                                $dialog.dialog('close');
                                if ($grid.data('treegrid')) {
                                    $grid.treegrid('reload')
                                } else {
                                    $grid.datagrid('reload');
                                }
                            } else {
                                $.messager.alert({
                                    title: '提示',
                                    msg: result.msg,
                                    icon: 'error'
                                });
                            }
                        }
                    });
                }
            },
            'close': {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    $.dialog.me(this).dialog('close');
                }
            },
            'search': {
                text: '查询',
                iconCls: 'icon-search',
                handler: function () {
                    var $dialog = $.dialog.me(this);
                    var $form = $.dialog.getForm(this);
                    var $table = $.dialog.me(this).dialog('options').grid;
                    $table.datagrid('load', $form.serializeObject());
                    $dialog.dialog('close');
                }
            },
            'clear': {
                text: '清除条件',
                iconCls: 'icon-clear',
                handler: function () {
                    var $form = $.dialog.getForm(this);
                    var $table = $.dialog.me(this).dialog('options').grid;
                    $form.form('clear');
                    $table.datagrid('load', {});
                }
            }
        }
    }
});


